package io.a;//package io.a;
//
//import org.jcodec.api.FrameGrab;
//import org.jcodec.api.JCodecException;
//import org.jcodec.common.Demuxer;
//import org.jcodec.common.DemuxerTrack;
////import org.jcodec.common.FileChannelWrapper;
////import org.jcodec.common.SeekableByteChannel;
//import org.jcodec.common.io.FileChannelWrapper;
//import org.jcodec.common.io.SeekableByteChannel;
//import org.jcodec.common.model.ColorSpace;
//import org.jcodec.common.model.Picture;
//
//import javax.imageio.ImageIO;
//import java.awt.image.BufferedImage;
//import java.io.File;
//import java.io.IOException;
//import java.nio.ByteBuffer;
//
//public class RTSPStreamCapture {
//
//    public static void main(String[] args) {
//        String rtspUrl = "rtsp://example.com/live/stream";
//        String outputPath = "output.jpg";
//
//        try {
//            SeekableByteChannel byteChannel = new FileChannelWrapper(new File(rtspUrl));
//            Demuxer demuxer = Demuxer.createDemuxer(demuxerFormat(byteChannel));
//            DemuxerTrack videoTrack = demuxer.getVideoTrack();
//
//            if (videoTrack != null) {
//                FrameGrab grab = FrameGrab.createFrameGrab(videoTrack, byteChannel);
//                Picture picture = grab.getNativeFrame();
//                if (picture != null) {
//                    Picture toSave = Picture.create(picture.getWidth(), picture.getHeight(), ColorSpace.RGB);
//                    toSave.copyFrom(picture);
//                    ImageIO.write(toBufferedImage(toSave), "jpg", new File(outputPath));
//                    System.out.println("Image saved successfully!");
//                } else {
//                    System.out.println("No frames found in the stream!");
//                }
//            } else {
//                System.out.println("No video track found in the stream!");
//            }
//        } catch (IOException | JCodecException e) {
//            e.printStackTrace();
//        }
//    }
//
//    private static DemuxerFormat demuxerFormat(SeekableByteChannel byteChannel) throws IOException {
//        ByteBuffer buffer = ByteBuffer.allocate(8);
//        byteChannel.setPosition(0);
//        byteChannel.read(buffer);
//        buffer.flip();
//        return DemuxerFormat.findFormat(buffer);
//    }
//
//    private static BufferedImage toBufferedImage(Picture picture) {
//        BufferedImage image = new BufferedImage(picture.getWidth(), picture.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
//        for (int y = 0; y < picture.getHeight(); y++) {
//            for (int x = 0; x < picture.getWidth(); x++) {
//                int rgb = picture.getRGB(x, y);
//                image.setRGB(x, y, rgb);
//            }
//        }
//        return image;
//    }
//}
